using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace The_Last_Knights.GameScreen
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ScreenManager : VisibleGameEntity
    {
        private static ScreenManager _data;

        private Dictionary<string, Screen> _screens;
        private string _activeIdScreen;

        private ScreenManager(Game game)
            : base(game)
        {
            _screens = new Dictionary<string, Screen>();
        }

        public static void Initialize(Game game)
        {
            if (_data == null)
                _data = new ScreenManager(game);
        }

        public static ScreenManager GetInstance()
        {
            return _data;
        }

        /// <summary>
        /// Them 1 doi tuong Screen vao Dictionary
        /// </summary>
        /// <param name="id"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddScreen(string id, Screen obj)
        {
            if (_screens.ContainsKey(id))
                return false;
            _screens.Add(id, obj);
            return true;
        }

        public bool RemoveScreen(string id)
        {
            if (_screens.ContainsKey(id))
            {
                _screens.Remove(id);
                return true;
            }
            return false;
        }

        public bool SwitchScreen(string id)
        {
            if (_screens.ContainsKey(id))
            {
                _activeIdScreen = id;
                return true;
            }
            return false;
        }

        public SpriteBatch GetSpriteBatch()
        {
            SpriteBatch sb = Game.Services.GetService(typeof (SpriteBatch)) as SpriteBatch;
            return sb;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            _screens[_activeIdScreen].Update(gameTime);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            _screens[_activeIdScreen].Draw(gameTime);
        }
    }
}
